package com.geely.config;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Component
public class RabbitmqConfig {

    //创建mq连接
    @Bean(name = "connectionFactory")
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();

        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");

        connectionFactory.setPublisherConfirms(true);

        //该方法配置多个host，在当前连接host down掉的时候会自动去重连后面的host
        connectionFactory.setAddresses("47.105.81.53:5672");
        return connectionFactory;
    }


    //监听处理类
    @Bean
    @Scope("prototype")
    public HandleService handleService() {
        return new HandleService();
    }

//    @Bean
//    //交换机类型fanout,topic,direct,handers
//    public void createDeadQueue() throws AmqpException, IOException  {
//        System.out.println("createDeadQueue");
////        connectionFactory().createConnection().createChannel(false).exchangeDeclare("monkey_exchange", "direct");
//        connectionFactory().createConnection().createChannel(false).exchangeDeclare("monkey_exchange", "topic", true, false, null);
//        connectionFactory().createConnection().createChannel(false).queueDeclare("monkey_queue", true, false, false, null);
//        connectionFactory().createConnection().createChannel(false).queueBind("monkey_queue", "monkey_exchange", "monkey.#");
//
//    }


    //动态创建queue，命名为：hostName.queue1【192.168.1.1.queue1】,并返回数组queue名称
    @Bean
    public String[] mqMsgQueues() throws AmqpException, IOException {
        System.out.println("mqMsgQueues 开始创建队列和绑定交换机");
        String[] queueNames = new String[1];
        String hostName = "topic";//获取hostName
        for (int i = 1; i <= 1; i++) {
            String queueName = String.format("%s.queue%d", hostName, i);
            //设置TTL 消息队列 过期时间， 不设置时，则填写null
            Map<String, Object> queueArgumentMap = new HashMap<>();
//            queueArgumentMap.put("x-message-ttl", "10000");
            connectionFactory().createConnection().createChannel(false).queueDeclare(queueName, true, false, false, null);
            connectionFactory().createConnection().createChannel(false).queueBind(queueName, "topicExchange", "topic.#");
            queueNames[i - 1] = queueName;
        }
        return queueNames;
    }

    //创建监听器，监听队列
    @Bean
    public SimpleMessageListenerContainer mqMessageContainer(HandleService handleService) throws AmqpException, IOException {
        System.out.println("mqMessageContainer");
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory());
        container.setQueueNames(mqMsgQueues());
        container.setExposeListenerChannel(true);
        container.setPrefetchCount(100);//设置每个消费者获取的最大的消息数量
        container.setConcurrentConsumers(4);//消费者个数
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);//设置确认模式为手工确认
        container.setMessageListener(handleService);//监听处理类
        return container;
    }

}
